package caidan;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import jinyilw.common.SettingTools;
import jinyilw.swing.rlaf.utils.RBorderFactory;
import jinyilw.swing.rlaf.utils.RLafUtils;
import jinyilw.swing.visuals.ButtonGroupPanel;
import jinyilw.swing.visuals.Rcomp.RDialog;

public class SetColDialog extends RDialog
{
	private static final long serialVersionUID = 6450501559874571615L;
	private ItemPanel[] panels;
	private JButton saveAndRestart;

	public SetColDialog(Window dialog)
	{
		super(dialog);
		setOptionStyle(150, 0.95f);
		setSection(8);
		setTitleHeight(28);
		setTitle("设置菜单列数");
		setTitleColor(Color.green);
		Container container = getContentPane();
		Container setContainer = new Container();
		Container buttonContainer = new JPanel();
		container.add(setContainer, BorderLayout.CENTER);
		container.add(buttonContainer, BorderLayout.SOUTH);
		setContainer.setLayout(new GridLayout(0, 1, 0, 2));
		panels = new ItemPanel[MainApp.cols.length];
		for (int i = 0; i < panels.length; i++)
		{
			panels[i] = new ItemPanel(i);
			setContainer.add(panels[i]);
		}

		JButton reset = new JButton("默认值");
		buttonContainer.add(reset);
		reset.addActionListener((ActionEvent e) ->
		{
			setSelect(MainApp.COL_NUMS);
		});

		JButton save = new JButton("保存");
		buttonContainer.add(save);
		save.addActionListener((ActionEvent e) ->
		{
			save();
			dispose();
		});
		saveAndRestart = new JButton("保存并重启");
		buttonContainer.add(saveAndRestart);
		saveAndRestart.addActionListener((ActionEvent e) ->
		{
			save();
			RLafUtils.restartApplication();
		});
//		JButton cancel = new JButton("取消");
//		buttonContainer.add(cancel);
//		cancel.addActionListener((ActionEvent e) ->
//		{
//			dispose();
//		});
		addActionListener((ActionEvent e) ->
		{
			saveAndRestart.setEnabled(true);
		});

		setResizable(false);
		// pack();
	}

	public void updateData()
	{
		for (int i = 0; i < panels.length; i++)
		{
			panels[i].select(MainApp.cols[i]);
			panels[i].updateName();
		}
		saveAndRestart.setEnabled(false);
		pack();
	}
	void setSelect(int[] cols)
	{
		for (int i = 0; i < panels.length; i++)
			panels[i].select(cols[i]);
	}
	public void save()
	{
		for (int i = 0; i < panels.length; i++)
			panels[i].saveData();
	}

	public void addActionListener(ActionListener listener)
	{
		for (int i = 0; i < panels.length; i++)
			panels[i].addActionListener(listener);;
	}

	class ItemPanel extends ButtonGroupPanel
	{
		private static final long serialVersionUID = 4063479596089385812L;
		String key;
		JLabel label;
		JRadioButton[] radioButtons;
		int nameIndex;

		public ItemPanel(int index)
		{
			key = MainApp.columnKey + index;
			nameIndex = index;
			RBorderFactory.setToActiveBorder2(this);
			// setBorder(RBorderFactory.getDefaultBorder());
			label = new JLabel();
			addOther(label);
			radioButtons = new JRadioButton[7];
			for (int i = 0; i < radioButtons.length; i++)
			{
				radioButtons[i] = new JRadioButton(String.valueOf(i));
				add(radioButtons[i]);
			}
		}
		public void updateName()
		{
			label.setText(MainApp.names[nameIndex] + ": ");
		}
		public void select(int i)
		{
			radioButtons[i].setSelected(true);
		}
		public void saveData()
		{
			SettingTools.putDefaultSettingData(key, getSelectIndex());
		}
	}
}
